<?php

namespace app\stock\controller\business;

use think\Controller;

class Business extends Controller
{
  public function _initialize()
  {
    parent::_initialize();
    $this->AdminModel = model('Admin');
    $this->ReceiveModel = model('Business.Receive');
    $this->BusinessModel = model('Business.Business');
    $this->OrderModel = model('Subject.Order');
  }

  public function receive()
  {
    $adminid = $this->request->param('adminid', 0, 'trim');

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->error('管理员不存在');
    }

    $data = $this->ReceiveModel->with('business')->where('applyid', $adminid)->order('applytime DESC')->select();


    $this->success('', '', $data);
  }

  public function businesslist()
  {
    $adminid = $this->request->param('adminid', 0, 'trim');

    $admininfo = $this->AdminModel->find($adminid);

    if (!$admininfo) {
      $this->error('管理员不存在');
    }

    $data = $this->BusinessModel->field('id,adminid,nickname')->where('adminid', $adminid)->order('id DESC')->select();

    $this->success('', '', $data);
  }

  public function orderlist()
  {
    $data = $this->OrderModel->with(['subject', 'business'])->order('createtime DESC')->select();

    $this->success('', '', $data);
  }

  public function orderDel()
  {
    $orderid = $this->request->param('orderid', 0, 'trim');

    $info = $this->OrderModel->find($orderid);

    if (!$info) {
      $this->error('课程不存在');
    }

    $result = $this->OrderModel->destroy($orderid);

    if (!$result) {
      $this->error('删除失败');
    }

    $this->success('删除成功');
  }
}
